package com.rcp.host;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rcp.objects.CustomerBean;

/**
 * Servlet implementation class AddCustomer
 */
@WebServlet("/AddCustomer")
public class AddCustomer extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	String className = "com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://cs3.calstatela.edu:3306/cs437group01";
	String dbusername = "cs437group01";
	String dbpassword = "VHj#dj!v";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddCustomer() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		ResultSet resultSet = null;
		
		try{
			
			// Include the driver class
			Class.forName( className );
			
			// Establish a connection to the database
			Connection connection =
				DriverManager.getConnection(url, dbusername, dbpassword);			
			
			// Get a reference to the statement object that will execute our query
			// on the server
			Statement statement = connection.createStatement();
			
			if(request.getParameter("edit") != null){
				
				String firstName = request.getParameter("firstName");
				String lastName = request.getParameter("lastName");
				String address = request.getParameter("address");
				String phone = request.getParameter("phone");
				String customerId = request.getParameter("customerId");
				
				statement.executeUpdate("UPDATE Customers SET FirstName = '" + firstName + "', LastName = '" + lastName + "', Address = '" + address + "', PhoneNumber = '" + phone + "' WHERE CustomerId = " + customerId);
				
				request.setAttribute("editsuccessful", true);
				
			}
			
			if(request.getParameter("editcustinfo") != null){
				
				String customerId = request.getParameter("editcustinfo");
				
				resultSet = statement.executeQuery("SELECT * FROM Customers WHERE CustomerID = " + customerId.trim());
				
				CustomerBean editCustomer = null;
				
				while(resultSet.next()){
					editCustomer = new CustomerBean(resultSet.getInt("CustomerID"), resultSet.getString("FirstName"), resultSet.getString("LastName"), resultSet.getString("Address"), resultSet.getString("PhoneNumber"));
				}
				
				resultSet = null;
				
				if(editCustomer != null){
					request.setAttribute("edit", true);
					request.setAttribute("editthiscustomer", editCustomer);
				}
				
			}
			
			if(request.getParameter("add") != null){

				String firstName = request.getParameter("firstName");
				String lastName = request.getParameter("lastName");
				String address = request.getParameter("address");
				String phone = request.getParameter("phone");
				
				statement.executeUpdate("INSERT INTO Customers (FirstName, LastName, Address, PhoneNumber) VALUES ('"+ firstName +"', '"+ lastName +"', '"+ address +"', '"+ phone +"')");
				
				request.setAttribute("addsuccessful", true);
				
			}
			
			if(request.getParameter("findcustomer") != null){
				
				String keyword = request.getParameter("keyword");
				
				resultSet = statement.executeQuery("SELECT * FROM Customers WHERE FirstName LIKE '%" + keyword + "%' OR LastName LIKE '%" + keyword + "%' OR Address LIKE '%" + keyword + "%' OR PhoneNumber LIKE '%" + keyword + "%'");
			
				ArrayList<CustomerBean> customers = new ArrayList<CustomerBean>();
				
				while(resultSet.next()){
					
					customers.add(new CustomerBean(resultSet.getInt("CustomerID"), resultSet.getString("FirstName"), resultSet.getString("LastName"), resultSet.getString("Address"), resultSet.getString("PhoneNumber")));
					
				}
				
				request.setAttribute("searched", true);
				request.setAttribute("customerresult", customers);
				
				resultSet = null;
				
			}
			
			
			// Close our DB Connection
			connection.close();
									
		}
		catch(SQLException e){		
			e.printStackTrace();
		}
		catch(ClassNotFoundException e){
			e.printStackTrace();
		}
		
		String pathToView = "/WEB-INF/host/AddCustomer.jsp";
		
		
		
		RequestDispatcher dispatcher =
				request.getRequestDispatcher( pathToView );
			
			dispatcher.forward(request, response);
			
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
